# -*- coding: utf-8 -*-
# @Time    : 2025/5/9 19:37
# @Author  : Z.J. Zhang
# @Email   : zijingzhang@mail.ustc.edu.cn
# @File    : mild_prebuncher_to_MAGIC.py
# @Software: PyCharm
from sw_to_MAGIC_commands import *
plt.ion()
sldprts = []

parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\THzDemonstration\100GHz\TTO_100GHz.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\TTO_GW\GW_TTO.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_6mm\04\MultigapExtractor\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_6mm\04\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_6mm\03\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_6mm\02\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_6mm\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_3mm\prebuncher2\forMAGIC\prebuncher2.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\channel_3mm\prebuncher1\forMAGIC\prebuncher.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\mild_prebuncher_and_dedispersion_buncher2\ForMAGIC\mild_prebuncher_and_dedispersion_buncher.ForMAGIC.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\mild_prebuncher_and_dedispersion_buncher4\mild_prebuncher_and_dedispersion_buncher4.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\mild_prebuncher_and_dedispersion_buncher.EIKO.SLDPRT\mild_prebuncher_and_dedispersion_buncher.EIKO.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\GeneratorAccelerator\geom\Genac2\ModerateBuncher\mild_prebuncher_and_dedispersion_buncher2\ForMAGIC\mild_prebuncher_and_dedispersion_buncher.ForMAGIC.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\dual_beam_dual_freq_HPM\egun\geom\yyh\egun.yyh.SLDPRT"
# parent_sldprt = r"E:\CSTprojects\dual_beam_dual_freq_HPM\egun\geom\egun.SLDPRT"
# parent_sldprt = r"D:\MagicFiles\HPM\电子枪部分\常规\电极形状20230720起.SLDPRT"
sldprts_dir, parent_sldprt_filename = os.path.split(parent_sldprt)
for file in os.listdir(sldprts_dir):
    if file.endswith(parent_sldprt_filename): continue  # 母文件
    if file.endswith('.SLDPRT') and not file.startswith("~$"):
        sldprts.append(os.path.join(sldprts_dir, file))
logger.info(sldprts)

# sldprts = [
#     r"D:\MagicFiles\HPM\12.5GHz\swCATHODE.sldprt",
#     r"D:\MagicFiles\HPM\12.5GHz\swANODE.sldprt",
#     r"D:\MagicFiles\HPM\12.5GHz\swEMITTER.sldprt",
#     r"D:\MagicFiles\HPM\12.5GHz\swPORT_LEFT.sldprt",
#     r"D:\MagicFiles\HPM\12.5GHz\swPORT_RIGHT.sldprt"
# ]

# stl_files = [r"D:\MagicFiles\HPM\12.5GHz\swCATHODE.STL",
#              r"D:\MagicFiles\HPM\12.5GHz\swANODE.STL",
#              r"D:\MagicFiles\HPM\12.5GHz\swEMITTER.STL",
#              r"D:\MagicFiles\HPM\12.5GHz\swPORT_LEFT.STL",
#              r"D:\MagicFiles\HPM\12.5GHz\swPORT_RIGHT.STL"
#              ]
# stl_files = [r"D:\MagicFiles\HPM\电子枪部分\阴极附近陶瓷\swANODE.STL",
#              r"D:\MagicFiles\HPM\电子枪部分\阴极附近陶瓷\swCATHODE.STL",
#              r"D:\MagicFiles\HPM\电子枪部分\阴极附近陶瓷\swEMITTER.STL",
#              r"D:\MagicFiles\HPM\电子枪部分\阴极附近陶瓷\swPORT_LEFT.STL",
#              r"D:\MagicFiles\HPM\电子枪部分\阴极附近陶瓷\swPORT_RIGHT.STL"
#              ]
important_dimensions = {}
info_base_file = "! Base file: %s\n"%parent_sldprt
area_cmd =  info_base_file +sldprt_to_MAGIC_commands(sldprts, axis_of_sketch=(Axis.z, Axis.y), important_dimensions =important_dimensions,re_export_to_stl=True)
grid_cmds= generate_grid_commands_to_keep_important_dimensions(important_dimensions["X2"],"X2",1e-3,0.07e-3)
cmd =area_cmd +"\n" *3 + grid_cmds
temp_file_to_save_MAGIC_comands = 'temp.m2d'

with open(temp_file_to_save_MAGIC_comands, 'w') as f:
    f.write(cmd)
    logger.info(cmd)

os.system('code %s' % (temp_file_to_save_MAGIC_comands,))
plt.legend()
# plt.gca().set_aspect('equal')
plt.show()

MAGIC_object_name_prefixs = legend_txt = [t.get_text() for t in plt.legend().get_texts()]
logger.info("Those commands might be also useful\n"
            "(X波段经验值。在这种网格密度下，若尺寸精准（不存在低于网格分辨率的MARK尺寸导致MERGE)，则频率的绝对值与COMSOL计算结果偏差在35 MHz左右，两腔相对频偏与COMSOL计算结果的偏差在5 MHz以内；\n"            
            "模拟cold test典型耗时~1 h):\n"
            "模拟hot test典型耗时~10 h (发射粒子区域网格dz = 1 mm时；若加密发射区域网格至dz = 0.4 mm，则典型耗时10 h)):\n"
            + "\n" * 3
            + "\n".join(
    ["VOID %s.0;" % MAGIC_object_name_prefix for MAGIC_object_name_prefix in MAGIC_object_name_prefixs])
            + "\n" * 3
            + "dz_recommended = 0.6mm;\n"
              "dr_recommended = 0.2mm;\n"
            + "\n".join(
    ["MARK %s.0 x1 size dz_recommended;" % MAGIC_object_name_prefix for MAGIC_object_name_prefix in MAGIC_object_name_prefixs])
            + "\n" * 3
            + "\n".join(
    ["MARK %s.0 x2 size dr_recommended;" % MAGIC_object_name_prefix for MAGIC_object_name_prefix in MAGIC_object_name_prefixs])
            )
plt.savefig("geom_view.png")
